package controller;

import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

import model.RatingScale;
import model.Student;
import model.Teacher;

/**
 * Created by BaiYunfei on 2017/6/14.
 */
public class RatingScaleController extends BaseController {

	public void list() {
		int role = getRole();
		SqlPara sqlPara = null;
		switch (role) {
		case 3:
			sqlPara = Db.getSqlPara("listRatingScale");
			break;
		case 2:
			Teacher t = Teacher.dao.findByUserId(getUserId());
			sqlPara = Db.getSqlPara("listRatingScaleByTeacher", t.getStr("Teacher_ID"));
			break;
		case 1:
			Student stu = Student.dao.findByUserId(getUserId());
			sqlPara = Db.getSqlPara("listRatingScaleByStudent", stu.getStr("Student_ID"));
			break;
		default:
			break;
		}

		Page<Record> scPage = Db.paginate(getPageNumber(), getPageSize(), sqlPara);
		renderDataTable(scPage);
	}
	
	public void save(){
		Map<String,String[]> paraMap = getParaMap();
		for (String key : paraMap.keySet()) {
			if(!key.startsWith("ID_")){
				continue;
			}
			if(paraMap.get(key).length == 0){
				continue;
			}
			String Teacher_ID = key.substring("ID_".length());
			String Student_ID = Student.dao.findByUserId(getUserId()).getStr("Student_ID");
			String value = paraMap.get(key)[0];
			if(value == null || "".equals(value.trim())){
				continue;
			}
			System.out.println("Teacher_ID:"+Teacher_ID+" Student_ID:"+Student_ID);
			RatingScale rs = new RatingScale();
			rs.set("Teacher_ID", Teacher_ID);
			rs.set("Student_ID", Student_ID);
			rs.set("Score", Integer.parseInt(value));
			rs.save();
		}
		renderJson();
	}

	public void index() {
		if (getRole() > 1) {
			render("ratingScale-list.html");
		} else {
			render("ratingScale-student-list.html");
		}
	}

}
